/*
 *      Copyright (c) 2018-2028, Joffee All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author:
 */
package com.sailmi.auth.granter;

import com.sailmi.auth.redis.RedisUtil;
import com.sailmi.common.tool.utils.WebUtil;
import com.sailmi.common.model.auth.AuthUser;
import jakarta.servlet.http.HttpServletRequest;
import com.sailmi.auth.enums.AuthUserEnum;
import com.sailmi.auth.utils.TokenUtil;
import com.sailmi.common.cache.CacheNames;
import com.sailmi.common.exception.ServiceException;
import com.sailmi.common.tool.utils.*;
import com.sailmi.common.secure.user.IUserProvider;
import org.springframework.stereotype.Component;

/**
 * 验证码TokenGranter
 *
 * @author yzh
 */
@Component
public class CaptchaTokenGranter implements ITokenGranter {
	public static final String GRANT_TYPE = "captcha";
	private final IUserProvider userProvider;
	private final RedisUtil redisUtil;

	public CaptchaTokenGranter(IUserProvider userProvider, RedisUtil redisUtil) {
		this.userProvider = userProvider;
		this.redisUtil = redisUtil;
	}

	@Override
	public AuthUser grant(TokenParameter tokenParameter) {
		HttpServletRequest request = WebUtil.getRequest();

		String key = request.getHeader(TokenUtil.CAPTCHA_HEADER_KEY);
		String code = request.getHeader(TokenUtil.CAPTCHA_HEADER_CODE);
		// 获取验证码
		String redisCode = String.valueOf(redisUtil.get(CacheNames.CAPTCHA_KEY + key));
		// 判断验证码
		if (code == null || !StringUtil.equalsIgnoreCase(redisCode, code)) {
			throw new ServiceException(TokenUtil.CAPTCHA_NOT_CORRECT);
		}

		String tenantId = tokenParameter.getArgs().getStr("tenantId");
		String account = tokenParameter.getArgs().getStr("account");
		String password = tokenParameter.getArgs().getStr("password");
		AuthUser userInfo = null;
		System.out.println("Now check account and account is:"+account);
		System.out.println("Now check account and password is:"+password);
		if (Func.isNoneBlank(account, password)) {
			// 获取用户类型
			String userType = tokenParameter.getArgs().getStr("userType");
			// 根据不同用户类型调用对应的接口返回数据，用户可自行拓展
			System.out.println("Now Get User Type:"+userType);
			if (userType.equals(AuthUserEnum.WEB.getName())) {
				System.out.println("11111");
				userInfo = userProvider.userInfo(tenantId, account, password);
			} else if (userType.equals(AuthUserEnum.APP.getName())) {
				System.out.println("22222");
				userInfo = userProvider.userInfo(tenantId, account, password);
			} else {
				System.out.println("33333");
				userInfo = userProvider.userInfo(tenantId, account, password);
			}
		}
		return userInfo;
	}

}
